package org.buptsse.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.buptsse.entity.CLHotPost;
import org.buptsse.entity.Circle;
import org.buptsse.entity.Comment;
import org.buptsse.entity.Friend;
import org.buptsse.entity.Messagebox;
import org.buptsse.entity.Post;
import org.buptsse.entity.Track;
import org.buptsse.entity.Tutelage;
import org.buptsse.entity.User;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author <a href="http://weibo.com/forresteternity">forrest</a>
 * 
 */

public class ActionUtils {
	private static final String TAG = "ActionUtils";
	private static final String URL_BASE="http://olimpic.sinaapp.com/";
	
	public String LoginAction(String url, String username, String password) {
		HttpClient client = new DefaultHttpClient();
		System.out.println("try already send data");
		try {
			HttpGet request = new HttpGet(new URI(URL_BASE+url) + "?name=" + username
					+ "&password=" + password);
			HttpResponse response = client.execute(request);
			System.out.println("already send data");
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String statusStr = EntityUtils.toString(entity, "UTF-8");
					Log.i(TAG, statusStr);
					if (statusStr != null && statusStr.startsWith("success"))
						return statusStr;
				}
			}
		} catch (Exception e) {
			Log.e(TAG, e.toString());
			return "exception";
		}
		return "error";
	}

	public String RegisterAction(String url, String email, String username,
			String password) {
		HttpClient client = new DefaultHttpClient();
		System.out.println("try already send data");
		try {
			HttpGet request = new HttpGet(new URI(URL_BASE+url) + "?mail=" + email
					+ "&name=" + username + "&password=" + password);
			HttpResponse response = client.execute(request);
			System.out.println("already send data");
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String statusStr = EntityUtils.toString(entity, "UTF-8");
					Log.i(TAG, statusStr);
					if (statusStr != null && statusStr.equals("success"))
						return "success";
				}
			}
		} catch (Exception e) {
			Log.e(TAG, e.toString());
			return "exception";
		}
		return "error";
	}

	/**
	 * @param url
	 * @param userId
	 * @param postcontent
	 * @param msgType
	 * @param CircleId
	 * @param CircleName
	 * @param repostId
	 * @param longtitude
	 * @param latitude
	 * @return status code
	 */
	public String PostAction(String url, int userId, String username,
			String postcontent, int msgType, int CircleId, String CircleName,
			int repostId, double longtitude, double latitude) {

		HttpPost request = new HttpPost(URL_BASE+url);
		JSONObject param = new JSONObject();
		StringEntity se = null;
		try {
			param.put("username", username);
			param.put("userId", userId);
			param.put("postcontent", postcontent);
			param.put("msgType", msgType);
			param.put("CircleId", CircleId);
			param.put("CircleName", CircleName);
			param.put("repostId", repostId);
			param.put("longtitude", longtitude);
			param.put("latitude", latitude);
			se = new StringEntity(param.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		request.setEntity(se);
		HttpResponse httpResponse = null;
		try {
			httpResponse = new DefaultHttpClient().execute(request);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String responseStr = null;
		try {
			responseStr = EntityUtils.toString(httpResponse.getEntity());
			System.out.println(responseStr);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (responseStr != null && responseStr.equals("success"))
			return responseStr;
		return responseStr;
	}

	/**
	 * 普通微博发送 平常事-1，地理位置-1，其他是ID号
	 * 
	 * @param url
	 * @param content
	 * @param userId
	 * @return
	 */
	public String PostAction(String url, String usrename, String content,
			int userId) {
		return PostAction(url, userId, usrename, content, -1, -1, "", -1, 0, 0);
	}

	public static boolean isNetworkAvailable(Activity mActivity) {
		System.out.println("isNetworkAvailable1");
		Context context = mActivity.getApplicationContext();
		System.out.println("isNetworkAvailable2");
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		System.out.println("isNetworkAvailable3");
		if (null == connectivity)
			return false;
		NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
		if (networkInfo == null || !networkInfo.isAvailable()) {
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 检测网络是否存在
	 */
	public static boolean HttpTest(final Activity mActivity) {
		System.out.println("httptest");
		if (!isNetworkAvailable(mActivity)) {
			showToast(mActivity);
			return false;
		}
		return true;
	}

	public static void showToast(Activity mActivity) {
		System.out.println("showToast");
		TextView tv = new TextView(mActivity);
		tv.setText("网路不存在");
		Toast toast = new Toast(mActivity);
		toast.setView(tv);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();
	}

	/**
	 * @param Url
	 * @param UserId
	 *            转发人的Id
	 * @param UserName
	 *            转发人的名字
	 * @param PostId
	 *            转发的微博的主键
	 */
	public void Repost(String Url, int UserId, String UserName, int PostId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("username", UserName);
			obj.put("postid", PostId);
			obj.put("messagetype", 1);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			System.out.println("after response response return ");
			if (response.getStatusLine().getStatusCode() == 200) {
				System.out.println("response reply");
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 发私信
	public void sendMessage(String Url, int FromId, int ToId, String content) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("toid", ToId);
			obj.put("fromid", FromId);
			obj.put("content", content);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 获取当个对象的信息，返回该对象的粉丝数，关注数，微博发表总数，与我的互粉关系，昵称,UserId是我的昵称，ObjectId是查找对象的Id
	public User FetchIndividualPersonInfor(String Url, int ObjectId,
			int UserId) {

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		User user = new User();
		JSONObject obj = new JSONObject();
		try {
			obj.put("userid", UserId);
			obj.put("objectid", ObjectId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");

				user.setId(object.getInt("userid"));
				user.setName(object.getString("name"));
				user.setFan(object.getInt("fan"));
				user.setFollow(object.getInt("follow"));
				user.setPostAmount(object.getInt("postamount"));
				user.setIsFollow(object.getBoolean("isfollow"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// 互粉hufen为true，则双方都互相关注了，如果为false，则仅仅是我关注了别人
	public List<Friend> fetchFollowInfor(String Url, int UserId) {
		System.out.println("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
		List<Friend> list = new ArrayList<Friend>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("followinfor");
				try {
					JSONObject tem = null;
					JSONArray array = object.getJSONArray("follow");
					for (int i = 0; i < array.length(); i++) {
						tem = array.getJSONObject(i);
						System.out.println("id:" + tem.getInt("id"));
						Friend friend = new Friend();
						friend.setUid(tem.getInt("id"));

						friend.setUname(tem.getString("name"));
						friend.setHufen(tem.getBoolean("flag"));
						friend.setPostamount(tem.getInt("postamount"));
						list.add(friend);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 删除被监护的信息，TutelageId是监护人的id，Userid是监护人自己的id
	public void DeleteTutelage(String Url, int TutelageId, int UserId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("tutelageid", TutelageId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			System.out.println("after response response return ");
			if (response.getStatusLine().getStatusCode() == 200) {
				System.out.println("response reply");
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:UserName是转发人的名字，UserId是转发人的Id,PostId是转发的微博的主键
	public void Repost(String Url, int UserId, String UserName, int PostId,
			String Content, int CircleId, String CircleName, double longitude,
			double latitude) {
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("content", Content);
			obj.put("username", UserName);
			obj.put("postid", PostId);
			obj.put("circlename", CircleName);
			obj.put("circleid", CircleId);
			obj.put("messagetype", 1);
			obj.put("longitude", longitude);
			obj.put("latitude", latitude);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			System.out.println("after response response return ");
			if (response.getStatusLine().getStatusCode() == 200) {
				System.out.println("response reply");
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// Url:/getFanInfor 返回粉丝的昵称，粉丝的趣闻条数，粉丝的id号
	// 供用户获取自己粉丝的信息，userid是用户自己的id号
	public List<User> GetFanInforsadf(String Url, int UserId) {
		List<User> list = new ArrayList<User>();

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONArray array = object.getJSONArray("fan");
				for (int i = 0; i < array.length(); i++) {
					User user = new User();
					obj = array.getJSONObject(i);
					user.setId(obj.getInt("id"));
					user.setPostAmount(obj.getInt("postamount"));
					user.setName(obj.getString("name"));
					user.setIsFollow(obj.getBoolean("flag"));
					list.add(user);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// Url:/deleteFan
	// 供用户删除恶意粉丝的函数，userid是用户的主id，fanid是粉丝的id
	public void DeleteFan(String Url, int UserId, int FanId) {

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("fanid", FanId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/getTrack
	// 供用户获取监护对象或者自己的在一定时间内的活动轨迹，HostId是监护对象的主id，begin是轨迹的开始时间段(要求时间是字符串，符合20110101121212这样的格式)
	public List<Track> GetTrack(String Url, int HostId, String Begin) {
		List<Track> list = new ArrayList<Track>();

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("hostid", HostId);
			obj.put("begin", Begin);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONArray array = object.getJSONArray("track");
				for (int i = 0; i < array.length(); i++) {
					Track track = new Track();
					obj = array.getJSONObject(i);
					track.setUserId(obj.getInt("hostid"));
					track.setLongitude(obj.getDouble("longitude"));
					track.setLatitude(obj.getDouble("latitude"));
					track.setTime(obj.getString("time"));
					list.add(track);
					System.out.println("userid:" + track.getUserId());
					System.out.println("longitude:" + track.getLongitude());
					System.out.println("latitude:" + track.getLatitude());
					System.out.println("time:" + track.getTime());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// Url:/storeTrack
	// 供用户记录自己的轨迹用，上传用户的经纬度，,userid是用户的主id
	public void StoreTrack(String Url, int UserId, double Longitude,
			double Latitude) {

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("longitude", Longitude);
			obj.put("latitude", Latitude);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/dealTutelageApply
	// 供用户处理监护信息的函数，userid是用户的主id，hostid是申请监护用户的人的id，username是用户自己的名字，allow是允许或不同意
	public void DealTutelageApply(String Url, int UserId, int HostId,
			String UserName, boolean Allow) {
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("hostid", HostId);
			obj.put("name", UserName);
			obj.put("allow", Allow);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString((entity), "utf8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/applyTutelage
	// 供用户申请监护他人时调用的函数，userid是用户自己的id，UserName是我的名字，hostid是监护对象的id号
	public void ApplyTutelage(String Url, int UserId, String userName,
			int HostId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("hostid", HostId);
			obj.put("username", userName);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString((entity), "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/getTutelageObject
	// 供用户获取监护对象的信息，传入的是用户自己的id号，返回监护对象的id号和名字
	public List<Tutelage> GetTutelageObject(String Url, int UserId) {
		List<Tutelage> list = new ArrayList<Tutelage>();

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONArray array = object.getJSONArray("tutelage");
				for (int i = 0; i < array.length(); i++) {
					Tutelage tu = new Tutelage();
					obj = array.getJSONObject(i);
					tu.setObjectId(obj.getInt("objectid"));
					System.out.println("objectid:" + tu.getObjectId());
					tu.setObjectName(obj.getString("objectname"));
					System.out.println("objectname:" + tu.getObjectName());
					list.add(tu);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Friend> FetchFollowInfor(String Url, int uid) {

		List<Friend> list = new ArrayList<Friend>();

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", uid);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("followinfor");
				JSONObject tem = null;
				JSONArray array = object.getJSONArray("follow");
				for (int i = 0; i < array.length(); i++) {
					tem = array.getJSONObject(i);
					Friend temp = new Friend();
					temp.setUname(tem.getString("name"));
					temp.setUid(tem.getInt("id"));
					temp.setHufen(tem.getBoolean("flag"));
					temp.setPostamount(tem.getInt("postamount"));
					list.add(temp);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("list:" + list.size());
		return list;

	}

	
	// Url:/deleteFollow
	// 供用户删除关注对象用，传入的是用户的id和将要删除的关注对象的id
	public void DeleteFollow(String Url, int UserId, int FollowId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("followid", FollowId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<org.buptsse.entity.Post> FetchFollowPost(String Url, int UserId) {
		List<org.buptsse.entity.Post> list = new ArrayList<org.buptsse.entity.Post>();
		
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity,"utf-8");
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				System.out.println(object);
				JSONArray array = object.getJSONArray("followpost");
				System.out.println(array);
				System.out.println(array.length());
				for (int i = 0; i < array.length(); i++) {
					org.buptsse.entity.Post post = new org.buptsse.entity.Post();
					obj = array.optJSONObject(i);
					post.setMessageType(obj.getInt("messagetype"));
					if (post.getMessageType() == 1) {// 1为转发，0为原创微博
						post.setHostId(obj.getInt("hostid"));// 存储被转发人的id
						post.setOriginalPostId(obj.getInt("originalpostid"));
						post.setOriginalPostContent(obj.getString("originalcontent"));
						post.setOriginalPostContent(obj.getString("originalusername"));
					}
					post.setPostId(obj.getInt("id"));
					post.setUserId(obj.getInt("userid"));
					post.setPostContent(obj.getString("content"));
					post.setPostTime(obj.getString("time"));
					post.setUsername(obj.getString("username"));
					post.setImage(obj.getString("image"));
					post.setRepostAmount(obj.getInt("repost"));
					post.setCommentsAmount(obj.getInt("commentamount"));
					list.add(post);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("list");
		System.out.println(list);
		return list;
	}

	// Url:/getPersonalInfor
	// 用户获取个人资料的函数，传入的是用户个人的主键,返回的是user对象，user中的某些属性被填充，如下
	public User GetPersonalInfor(String Url, int UserId) {
		User user = new User();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				user.setAge(object.getInt("age"));
				user.setBirthday(object.getString("birthday"));
				user.setHometown(object.getString("hometown"));
				user.setSex(object.getString("sex"));
				user.setPassword(object.getString("password"));
				user.setSecureScope(object.getInt("securescope"));
				user.setName(object.getString("name"));

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// Url:/setPersonalInfor
	// 个人资料设置，每次设置时先获取数据库中个人的资料设置情况，用户在修改了自己的资料后调用该函数
	public void SetPersonalInfor(String Url, int UserId, double SecureScope,
			String Name, int Age, String BirthDay, String Password, String Sex,
			String Hometown) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("name", Name);
			obj.put("age", Age);
			obj.put("birthday", BirthDay);
			obj.put("password", Password);
			obj.put("sex", Sex);
			obj.put("scope", SecureScope);
			obj.put("hometown", Hometown);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/getSurroundPost
	// 获取在该经纬度下其他用户在此处的标记信息，标记信息默认存为circleid为-2的圈子，scope为设定半径
	public List<Post> GetSurroundPost(String Url, double Longitude,
			double Latitude, int scope) {
		List<Post> list = new ArrayList<Post>();

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("longitude", Longitude);
			obj.put("latitude", Latitude);
			obj.put("scope", scope);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONArray array = object.getJSONArray("surroundpost");
				System.out.println("array.size():" + array.length());
				for (int i = 0; i < array.length(); i++) {
					Post post = new Post();
					obj = array.optJSONObject(i);
					post.setPostId(obj.getInt("postid"));
					System.out.println("postid:" + post.getPostId());
					post.setPostContent(obj.getString("content"));
					System.out.println("content:" + post.getPostContent());
					post.setPostTime(obj.getString("time"));
					post.setUserId(obj.getInt("userid"));
					post.setLongitude(obj.getDouble("longitude"));
					post.setLatitude(obj.getDouble("longitude"));
					System.out.println("userid:" + post.getUserId());
					post.setDistance(obj.getDouble("distance"));
					System.out.println("distance:" + post.getDistance());
					list.add(post);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// Url:/getMyPost
	// 获取本人发表的所有微博，这种获取方式不对，后期考虑更改
	public List<Post> GetMyPost(String Url, int UserId) {
		List<Post> list = new ArrayList<Post>();

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			request.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			System.out.println("准备发送数据！");
			HttpResponse response = client.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString((entity), "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("array");
				System.out.println(object.toString());
				System.out.println("kkkkkkkkkkkkkkkkkkkkkkk");
				JSONArray array = object.getJSONArray("mypost");
				System.out.println("mypost:" + array.toString());
				for (int i = 0; i < array.length(); i++) {
					System.out.println("kkkkkkkkkkkkkkkkkkkkkkk");
					Post post = new Post();
					obj = array.getJSONObject(i);
					post.setMessageType(obj.getInt("messagetype"));
					if (post.getMessageType() == 1) {// 1为转发，0为原创微博
						post.setHostId(obj.getInt("hostid"));// 存储被转发人的id
						System.out.println("hostid:" + post.getHostId());
						post.setOriginalPostId(obj.getInt("originalpostid"));
						System.out.println("originalPostid:"
								+ post.getOriginalPostId());
						post.setOriginalPostContent(obj
								.getString("originalpostcontent"));
						post.setOriginalUsername(obj
								.getString("originalusername"));
					}
					post.setPostId(obj.getInt("postid"));
					System.out.println("postid:" + post.getPostId());
					post.setPostContent(obj.getString("content"));
					System.out.println("content:" + post.getPostContent());
					post.setPostTime(obj.getString("time"));
					System.out.println("time:" + post.getPostTime());
					post.setRepostAmount(obj.getInt("repost"));
					System.out.println("repost:" + post.getRepostAmount());
					post.setCommentsAmount(obj.getInt("commentamount"));
					post.setCircleName(obj.getString("circlename"));
					System.out.println("circlename:" + post.getCircleName());
					System.out.println("commentamount:"
							+ post.getCommentsAmount());
					post.setCircleId(obj.getInt("circleid"));
					list.add(post);
				}
			} else {
				System.out.println("response 返回失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// Url:/fetchComment
	// 供用户获取某条微博的对应的评论，返回评论人id，评论人姓名，时间，和言论，比较客户端的主人id与评论人id，相同则增加删除评论的按钮
	public List<Comment> FetchComment(String Url, int PostId) {
		List<Comment> list = new ArrayList<Comment>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("postid", PostId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString((entity), "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONArray array = object.getJSONArray("comment");
				for (int i = 0; i < array.length(); i++) {
					Comment comment = new Comment();
					obj = array.getJSONObject(i);
					comment.setComment(obj.getString("comment"));
					System.out.println("Comment:" + comment.getComment());
					comment.setCommentId(obj.getInt("commentid"));
					System.out.println("CommentId:" + comment.getCommentId());
					comment.setPostId(obj.getInt("postid"));
					System.out.println("PostId:" + comment.getPostId());
					comment.setCommentPeopleId(obj.getInt("commentpeopleid"));
					System.out.println("CommentPeopleId:"
							+ comment.getCommentPeopleId());
					comment.setCommentPeopleName(obj
							.getString("commentpeoplename"));
					System.out.println("CommentPeopleName:"
							+ comment.getCommentPeopleName());
					comment.setTime(obj.getString("commenttime"));
					System.out.println("time:" + comment.getTime());
					list.add(comment);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("list.size():" + list.size());
		return list;
	}

	// Url:/deleteComment
	// 当用户是评论的微博的主人或者是该评论的持有者时，可以删除该评论，在此需要客户端执行严格的检验以标记删除按钮
	public void DeleteComment(String Url, int CommentId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("commentid", CommentId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				String responseStr = EntityUtils.toString(response.getEntity(),
						"utf8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/addComment
	// 供用户评论他人的微博用，PostId是评论的微博的id，Userid是微博的发表人的Id，CommentPeopleId是评论人的ID，MyName是评论人的名字，Comment是评论人的评论内容
	public boolean AddComment(String Url, int PostId, int UserId,
			int CommentPeopleId, String CommentPeopleName, String Comment,
			int CircleId, String CircleName) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("postid", PostId);
			obj.put("userid", UserId);
			obj.put("commentpeopleid", CommentPeopleId);
			obj.put("commentpeoplename", CommentPeopleName);
			obj.put("comment", Comment);
			obj.put("circleid", CircleId);
			obj.put("circlename", CircleName);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				String responseStr = EntityUtils.toString(response.getEntity(),
						"utf8");
				if(null!=responseStr && "success".equals(responseStr)){
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// Url:/deletePost
	// 供微博发表人删除微博用，postid是微博的主键
	public void DeletePost(String Url, int PostId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("postid", PostId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Messagebox> FetchMessageBox(String Url, int UserId) {

		List<Messagebox> list = new ArrayList<Messagebox>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONArray array = object.getJSONArray("message");
				JSONObject tem = null;
				for (int i = 0; i < array.length(); i++) {
					Messagebox box = new Messagebox();
					tem = array.getJSONObject(i);
					box.setFromName(tem.getString("fromname"));
					box.setFromId(tem.getInt("fromid"));
					box.setContent(tem.getString("message"));
					box.setMessageType(tem.getInt("messagetype"));
					box.setTime(tem.getString("time"));
					list.add(box);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// Url:/getCirclePost
	// 获取群内成员发表的言论CircleId 是圈的ID
	public List<Post> GetCirclePost(String Url, int CircleId) {

		List<Post> postlist = new ArrayList<Post>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("circleid", CircleId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity,"utf-8");
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("head");
				JSONArray array = object.getJSONArray("post");
				for (int i = 0; i < array.length(); i++) {
					Post temp = new Post();
					obj = array.getJSONObject(i);
					temp.setMessageType(obj.getInt("messagetype"));
					if (temp.getMessageType() == 1) {// 1为转发，0为原创微博
						temp.setHostId(obj.getInt("id"));// 存储被转发人的id
						temp.setOriginalPostId(obj.getInt("originalpostid"));
					}
					temp.setPostId(obj.getInt("postid"));
					temp.setPostContent(obj.getString("content"));
					temp.setPostTime(obj.getString("time"));
					temp.setUserId(obj.getInt("userid"));
					temp.setRepostAmount(obj.getInt("repost"));
					temp.setCommentsAmount(obj.getInt("commentamount"));
					postlist.add(temp);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return postlist;
	}

	// Url:/deleteCirclePeople
	// 供圈主删除圈员用，userid是删除对象的主id号
	public void DeleteCirclePeople(String Url, int CircleId, int UserId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("circleid", CircleId);
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// Url:/getCirclePeopleInfor
	// 获取该组内成员的信息，返回时成员的昵称，粉丝数，关注数，微博发表的总数将会被填充
	public List<User> GetCirclePeopleInfor(String Url, int CircleId) {
		List<User> user = new ArrayList<User>();
		User temp = new User();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("circleid", CircleId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("head");
				JSONArray json = object.getJSONArray("json");
				for (int i = 0; i < json.length(); i++) {
					obj = json.getJSONObject(i);
					temp.setName(obj.getString("username"));
					temp.setFan(obj.getInt("fan"));
					temp.setFollow(obj.getInt("follow"));
					temp.setPostAmount(obj.getInt("postamount"));
					temp.setId(obj.getInt("userid"));
					user.add(temp);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// Url:/getApplyPersonInfor,查看加圈人的信息
	public User GetApplyPersonInfor(String Url, int UserId) {// 圈主在获取申请信息后查看用户的个人信息，需要传入申请人的id
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		;
		JSONObject obj = new JSONObject();
		User user = new User();
		try {
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("personinfor");

				user.setFan(object.getInt("fan"));
				user.setFollow(object.getInt("follow"));
				user.setPostAmount(object.getInt("postamount"));
				user.setName(object.getString("username"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// Url:/joinCircle
	// 圈主处理请求的函数，hostid：圈主id，userid：申请人id。allow 布尔值，true
	// 同意，false反对，CircleId：圈子的id
	public void JoinCircle(String Url, int UserId, int HostId, int CircleId,
			boolean Allow) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("userid", UserId);
			obj.put("hostid", HostId);
			obj.put("circleid", CircleId);
			obj.put("allow", Allow);
			pair.add(new BasicNameValuePair("json", obj.toString()));// 此处的json就是该对象的key值，在服务器端的response就需要再指定一次，这里不需要
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				String responseStr = EntityUtils.toString(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/post
	// circleid为2，往关注对象发，1往周围圈子发
	public int SendPost(String url, int UserId, String content, int circleid,
			String CircleName, double Longitude, double Latitude) {
		System.out.println("send "+content+" now");
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject json = new JSONObject();
		try {
			json.put("content", content);
			json.put("id", UserId);
			json.put("circlename", CircleName);
			json.put("circleid", circleid);
			json.put("messagetype", 0);
			json.put("longitude", Longitude);
			json.put("latitude", Latitude);
			Date date = new Date();
			pair.add(new BasicNameValuePair("json", json.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			String responseStr = EntityUtils.toString(response.getEntity());
			System.out.println("return a number "+responseStr);
			int responseInt = Integer.parseInt(responseStr);
			//responseInt预留给图像上传使用
			return responseInt;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	// Url:/applyCircle
	// UserId 申请人ID，HostId 圈主
	public void ApplyCircle(String Url, int UserId, int HostId, int CircleId,
			String CircleName, String UserName) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		Map map = new HashMap<String, Object>();
		map.put("hostid", HostId);
		map.put("circleid", CircleId);
		map.put("circlename", CircleName);
		map.put("username", UserName);
		map.put("userid", UserId);
		try {
			pair.add(new BasicNameValuePair("json", map.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Url:/searchCircle,当无搜索结果时，返回null
	public List<Circle> SearchCircle(String url, String CircleType,
			String CircleName)// 按照圈类型搜索圈子,圈子名称，圈id，圈类型和圈总人数会被填充
	{
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+url);
		List<Circle> circle = new ArrayList<Circle>();
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		try {
			JSONObject obj = new JSONObject();
			obj.put("circlename", CircleName);
			obj.put("circletype", CircleType);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String responseStr = EntityUtils.toString(entity, "utf8");
				System.out.println("responseStr:" + responseStr);
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");// 此处可能有问题
				System.out.println(responseStr);

				JSONArray array = object.getJSONArray("circle");
				if (array.length() == 0)
					return null;
				for (int i = 0; i < array.length(); i++) {
					Circle temp = new Circle();
					JSONObject tem = (JSONObject) array.opt(i);
					temp.setCircleId(tem.getInt("circleid"));
					temp.setCircleName(tem.getString("circlename"));
					temp.setCircleType(tem.getString("circletype"));
					temp.setAmount(tem.getInt("amount"));
					temp.setUserId(tem.getInt("hostid"));
					circle.add(temp);

				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return circle;

	}

	/**
	 * @param Url
	 *            :fetchJoinCircle
	 * @param UserId
	 *            :用户id
	 * @return
	 */
	public List<Circle> FetchJoinCircle(String Url, int UserId) {
		List<Circle> list = new ArrayList<Circle>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://olimpic.sinaapp.com/allchannel.php");
		System.out.println("start http://");
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject json = new JSONObject();
		try {
			json.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", json.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			String responseStr = EntityUtils.toString(entity, "utf8");
			System.out.println("responseStr:" + responseStr);
			if (response.getStatusLine().getStatusCode() == 200) {
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONObject tem = null;
				JSONArray array = object.getJSONArray("circle");
				for (int i = 0; i < array.length(); i++) {
					Circle circle = new Circle();
					tem = array.getJSONObject(i);
					circle.setCircleId(tem.getInt("circleID"));
					circle.setCircleName(tem.getString("circleName"));
					circle.setDescription("description");
					circle.setLevel(1);
					list.add(circle);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// Url:/createCircle
	public void CreateCircle(String url, int UserId, String CircleName,
			String Description, String CircleType) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject json = new JSONObject();
		try {
			json.put("id", UserId);
			json.put("description", Description);
			json.put("circlename", CircleName);
			json.put("circletype", CircleType);
			pair.add(new BasicNameValuePair("json", json.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			String responseStr = EntityUtils.toString(entity);
			if (response.getStatusLine().getStatusCode() == 200) {
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public User login(String urlAddress, String mail, String password) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+urlAddress);
		List<NameValuePair> Pair = new ArrayList<NameValuePair>();
		JSONObject ob = new JSONObject();
		try {
			ob.put("mail", mail);
			ob.put("password", password);

			Pair.add(new BasicNameValuePair("json", ob.toString()));
			post.setEntity(new UrlEncodedFormEntity(Pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			String responseStr = EntityUtils.toString(response.getEntity());
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity out = response.getEntity();
				if (out != null) {
					if (responseStr == null){}
					else {
						JSONObject obj = new JSONObject(responseStr)
								.getJSONObject("json");
						if (obj.getInt("result") == 1) {
							User user = new User();
							user.setId(obj.getInt("userid"));
							user.setName(obj.getString("name"));
							user.setFan(obj.getInt("fan"));
							user.setFollow(obj.getInt("follow"));
							user.setPostAmount(obj.getInt("postamount"));
							return user;
						}

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	//fetchPersonalMessage获取个人的收件箱//fetchSystemMessage获取系统通知//fetchPostMessage获取曾经发送的消息
	public List<Messagebox> FetchMessagebox(String Url,int UserId)
	{
		
		List<Messagebox> list=new ArrayList<Messagebox>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+Url);
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject obj = new JSONObject();
		try {
			obj.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", obj.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity=response.getEntity();
				String responseStr=EntityUtils.toString(entity,"utf8");
				System.out.println("responseStr:"+responseStr);
				JSONObject object=new JSONObject(responseStr).getJSONObject("json");
				JSONArray array=object.getJSONArray("message");
				JSONObject tem=null;
				for(int i=0;i<array.length();i++)
				{
					Messagebox box=new Messagebox();
					tem=array.getJSONObject(i);
					box.setFromName(tem.getString("fromname"));
					box.setFromId(tem.getInt("fromid"));
					box.setContent(tem.getString("message"));
					box.setMessageType(tem.getInt("messagetype"));
					box.setTime(tem.getString("time"));
					list.add(box);
				}
			}
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		return list;
	}
	/*
	 * 
	 * lf
	 * 
	 * 
	 * 
	 */
	/**
	 * @param Url
	 *            :myHot //我的热点
	 * @param UserId
	 *            :用户id
	 * @return
	 */
	public List<CLHotPost> myHot(String Url, int UserId) {
		List<CLHotPost> list = new ArrayList<CLHotPost>();
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(URL_BASE+"myHot.php");
		System.out.println("start http://");
		List<NameValuePair> pair = new ArrayList<NameValuePair>();
		JSONObject json = new JSONObject();
		try {
			json.put("userid", UserId);
			pair.add(new BasicNameValuePair("json", json.toString()));
			post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			String responseStr = EntityUtils.toString(entity, "utf8");
			System.out.println("responseStr:" + responseStr);
			if (response.getStatusLine().getStatusCode() == 200) {
				JSONObject object = new JSONObject(responseStr)
						.getJSONObject("json");
				JSONObject tem = null;
				JSONArray array = object.getJSONArray("hotpost");
				for (int i = 0; i < array.length(); i++) {
					CLHotPost hotPost = new CLHotPost();
					tem = array.getJSONObject(i);
					
					hotPost.setHotChannel(tem.getString("hotChannel"));
					hotPost.setHotName(tem.getString("hotName"));
					hotPost.setHotType(tem.getInt("hotType"));
					hotPost.setPostData(tem.getString("data"));
					
					list.add(hotPost);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	

}
